package com.zzy.chapter3.deprecated.state;

import com.zzy.chapter3.utils.RedisCommonProcessor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class DeprecatedReceiveOrder extends DeprecatedAbstractOrderState {

    @Autowired
    private RedisCommonProcessor redisCommonProcessor;

    @Autowired
    private DeprecatedAbstractOrderState orderState;

    @Override
    protected DeprecatedOrder receiveOrder(String orderId) {
        // 1. 从redis中获取订单
        DeprecatedOrder order = (DeprecatedOrder) redisCommonProcessor.get(orderId);
        if (!order.getState().equals(ORDER_WAIT_RECEIVE)) {
            throw new RuntimeException("订单状态异常");
        }
        // 2. 用户收货后，设置订单状态为"订单完成"，并删除redis缓存
        order.setState(ORDER_FINISH);
        redisCommonProcessor.remove(orderId);
        // 3. 观察者模式：发送订单收货event
        orderState.notifyObserve(orderId, ORDER_FINISH);
        return order;
    }
}
